#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>


int main(void)
{
	int read_fd;
	int write_fd;
	int file_len=0;
	int i=0;
	char buffer[3];
	char buf1[2];
	int tempH=0;
	int tempL=0;
	int frame=0;
	
	
	unsigned char audio;
	read_fd = open("./test.txt", O_RDONLY);
	if(read_fd <0)
	{
		printf("read open wrong\n");
		exit(1);
	}
	
	write_fd = open("./out.pcm", O_WRONLY|O_CREAT);
	if(write_fd <0)
	{
		printf("write open wrong\n");
		exit(1);
	}
	char str;
	sprintf(&str, "%c", 128); 
	printf("----%c\n" , str);
	/*测得文件大小*/
 	file_len= lseek(read_fd,0L,SEEK_END);
    lseek(read_fd,0L,SEEK_SET);
    printf("form file size is %d\n",file_len);

     	
    for(i = 0 ; i<(file_len/3) ; i++)
   //for(i = 0 ; i< 9 ; i++)
    {
    	 memset(buffer,0,sizeof(buffer));
   		 read(read_fd ,buffer,sizeof(buffer)); 		 
//   		 printf("read_string:%s \n", buffer);
   		 memset(buf1,0,sizeof(buf1));
   		 sscanf(&buffer[0],"%[^ ] ",buf1);
   		 if(buf1[0] < 65)
   		 {
   		 	tempH = (buf1[0] - 48) * 16;
   		 }
   		 else
   		 {
   		 	tempH = (buf1[0] - 55) * 16;
   		 }  
   		 if(buf1[1] < 65 ) 	
   		 {	 
		 	tempL = buf1[1] - 48;
		 }
		 else
		 {
		 	tempL = buf1[1] - 55;
		 }
		 frame = tempH+tempL;
//   		 printf("%d---%d----%d\n",tempH,tempL,frame);

		sprintf(&audio,"%c",frame);
		write(write_fd,&audio,1);   		 
   	}
    
    close(read_fd);
	close(write_fd);
	
	
	
}
